package cn.ittaoli.szdz.controller;

import cn.ittaoli.szdz.model.DzJbxx;
import cn.ittaoli.szdz.model.DzJbxxXq;
import cn.ittaoli.szdz.model.VDzBmzs;
import cn.ittaoli.szdz.service.DzJbxxService;
import cn.ittaoli.szdz.service.SingleService;
import cn.ittaoli.utils.ExcelUtils;
import cn.ittaoli.utils.ExportExcel;
import cn.ittaoli.utils.FileUpload;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;

/**
 * Created by lub_lenovo on 2017/8/18.
 */
@Controller
@RequestMapping("/single")
public class SingleController {

    @Autowired
    private SingleService singleService;

    @Autowired
    private DzJbxxService dzJbxxService;



    @RequestMapping("/index")
    public String index(Model model){
        List<VDzBmzs> vDzBmzs=singleService.selectAll();
        List<VDzBmzs> sqBmzs=singleService.selectSQBmzs();
        model.addAttribute("qx",vDzBmzs);
        model.addAttribute("sq",sqBmzs);
        return "/single/index";
    }




    @RequestMapping("/getData")
    @ResponseBody
    public Map<String,Object> getData(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "20") Integer rows,@RequestParam(defaultValue = "") String q){
        Map<String,Object> map=new HashMap<>();

        PageInfo<DzJbxxXq> dzJbxxPageInfo=dzJbxxService.getData(page,rows,q);
        Long records=dzJbxxPageInfo.getTotal();
        map.put("rows",dzJbxxPageInfo.getList());
        map.put("records",records);
        map.put("total",dzJbxxPageInfo.getPages());
        map.put("page",dzJbxxPageInfo.getPageNum());
        return map;

    }


    @RequestMapping("/detail")
    public String detail(String ksh,Model model){
        DzJbxx dzJbxx=dzJbxxService.selectByKsh(ksh);
        model.addAttribute("res",dzJbxx);
        return "/single/detail";
    }


    @RequestMapping("/addbscj")
    public String addbscj(){
        return "/single/addbscj";
    }

    @RequestMapping("/upexcel")
    public String upexcel(Model model,@RequestParam MultipartFile file, HttpServletRequest request) throws IOException {
        Integer res=singleService.upExcel(file,request);
        if(res>0){
            return "redirect:/single/index";
        }else{
            model.addAttribute("msg","导入失败");
            return "index";
        }
    }



    @RequestMapping("/exportExcel")
    public String exportExcel(HttpServletResponse response) throws Exception {

        // 初始化HttpServletResponse对象
//        HttpServletResponse response = response..getResponse();

        // 定义表的标题
        String title = "员工列表一览";

        //定义表的列名
        String[] rowsName = new String[] { "考生号", "姓名", "身份证号", "语文成绩", "数学成绩","外语成绩"};

        //定义表的内容
        List<Object[]> dataList = new ArrayList<Object[]>();

        Object[] objs = null;
        List<DzJbxx> dzJbxxes = singleService.selectAllJbxx();


        for (int i = 0; i < dzJbxxes.size(); i++) {
            DzJbxx per = dzJbxxes.get(i);
            objs = new Object[rowsName.length];
            objs[0] = per.getKsh();
            objs[1] = per.getXm();
            objs[2] = per.getSfzh();
            objs[3] = per.getYwcj();
            objs[4] = per.getSxcj();
            objs[5] = per.getWycj();
//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//            String date = df.format(per.getJobtime());
//            objs[5] = date;
//            objs[6] = per.getResume();
//            objs[7] = per.getFilepath();
//            objs[8] = per.getDept().getDname();
            dataList.add(objs);
        }

        // 创建ExportExcel对象
        ExportExcel ex = new ExportExcel(title, rowsName, dataList);

            // 输出Excel文件
            try {
                OutputStream output = response.getOutputStream();
                response.reset();
                response.setHeader("Content-disposition",
                        "attachment; filename=PersonList.xls");
                response.setContentType("application/msexcel");
                ex.export(output);
                output.close();
            } catch (IOException e) {
            e.printStackTrace();
        }

        return "tolist";// 返回列表显示
    }


}
